<template>
  <el-container class="layout-container">
    <!-- 侧边栏 -->
    <el-aside width="240px" class="sidebar">
      <div class="logo">
        <el-icon class="logo-icon"><Monitor /></el-icon>
        <span class="logo-text">Stream Screen</span>
      </div>

      <div class="sidebar-scroll">
        <el-menu
          :default-active="$route.path"
          class="sidebar-menu"
          router
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
          :default-openeds="[]"
        >






          <!-- 预警中心子菜单 -->
          <el-sub-menu index="alert">
            <template #title>
              <el-icon><Warning /></el-icon>
              <span>预警中心</span>
            </template>
            <el-menu-item index="/alert/report">
              <el-icon><Document /></el-icon>
              <span>预警统计报表</span>
            </el-menu-item>
            <el-menu-item index="/alert/report2">
              <el-icon><Document /></el-icon>
              <span>预警实时看板</span>
            </el-menu-item>
            <el-menu-item index="/alert/list">
              <el-icon><List /></el-icon>
              <span>预警列表</span>
            </el-menu-item>
          </el-sub-menu>

          <!-- 行为监控子菜单 -->
          <el-sub-menu index="behavior">
            <template #title>
              <el-icon><View /></el-icon>
              <span>行为监控</span>
            </template>
            <el-menu-item index="/behavior/analysis">
              <el-icon><TrendCharts /></el-icon>
              <span>鼠标键盘操作趋势分析</span>
            </el-menu-item>
            <el-menu-item index="/behavior/recording">
              <el-icon><VideoCamera /></el-icon>
              <span>录屏记录管理</span>
            </el-menu-item>
            <el-menu-item index="/behavior/screenshot">
              <el-icon><Picture /></el-icon>
              <span>截屏记录管理</span>
            </el-menu-item>

            <el-menu-item index="/behavior/screen-sharing">
              <el-icon><VideoCamera /></el-icon>
              <span>屏幕共享</span>
            </el-menu-item>
          </el-sub-menu>
          <!-- 员工电脑端配置管理子菜单 -->
          <el-sub-menu index="client">
            <template #title>
              <el-icon><Monitor /></el-icon>
              <span>员工电脑端配置管理</span>
            </template>
            <el-menu-item index="/client/config">
              <el-icon><Setting /></el-icon>
              <span>管理规则配置</span>
            </el-menu-item>
            <el-menu-item index="/client/list">
              <el-icon><List /></el-icon>
              <span>客户端配置</span>
            </el-menu-item>

          </el-sub-menu>
          <!-- 摄像头设备管理子菜单 -->
          <el-sub-menu index="camera">
            <template #title>
              <el-icon><Camera /></el-icon>
              <span>摄像头设备管理</span>
            </template>
            <el-menu-item index="/camera/device">
              <el-icon><Monitor /></el-icon>
              <span>设备管理</span>
            </el-menu-item>
            <el-menu-item index="/camera/alert">
              <el-icon><Warning /></el-icon>
              <span>报警管理</span>
            </el-menu-item>
            <el-menu-item index="/camera/algorithm">
              <el-icon><Setting /></el-icon>
              <span>算法配置</span>
            </el-menu-item>
            <el-menu-item index="/camera/video">
              <el-icon><VideoCamera /></el-icon>
              <span>视频预览</span>
            </el-menu-item>
          </el-sub-menu>



          <!-- 系统设置子菜单 -->
          <el-sub-menu index="settings">
            <template #title>
              <el-icon><Setting /></el-icon>
              <span>系统设置</span>
            </template>
            <el-menu-item index="/settings/user">
              <el-icon><User /></el-icon>
              <span>用户与部门管理</span>
            </el-menu-item>

            <el-menu-item index="/settings/deployment">
              <el-icon><Download /></el-icon>
              <span>安装部署管理</span>
            </el-menu-item>
<!--            <el-menu-item index="/settings/audit">-->
<!--              <el-icon><Notebook /></el-icon>-->
<!--              <span>系统日志审计</span>-->
<!--            </el-menu-item>-->
            <!-- <el-menu-item index="/api-test">
              <el-icon><Tools /></el-icon>
              <span>API测试</span>
            </el-menu-item> -->
          </el-sub-menu>
        </el-menu>
      </div>
    </el-aside>

    <!-- 主内容区 -->
    <el-container>
      <!-- 顶部导航栏 -->
      <el-header class="header">
        <div class="header-left">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item>Stream Screen</el-breadcrumb-item>
            <el-breadcrumb-item>{{ currentPageTitle }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>

        <div class="header-right">
          <!-- WebSocket连接状态 -->
          <!-- <el-tooltip :content="connectionStatusText" placement="bottom">
            <el-badge
              :value="connectionStatus === 'connected' ? '' : '!'"
              :hidden="connectionStatus === 'connected'"
              class="connection-badge"
            >
              <el-icon
                :class="['connection-icon', connectionStatus]"
                :color="connectionStatusColor"
              >
                <Connection />
              </el-icon>
            </el-badge>
          </el-tooltip> -->

          <!-- 用户菜单 -->
          <el-dropdown class="user-dropdown" @command="handleUserCommand">
            <span class="user-info">
              <el-icon><User /></el-icon>
              <span>{{ authStore.userInfo?.username || '管理员' }}</span>
              <el-icon class="el-icon--right"><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">
                  <el-icon><User /></el-icon>
                  个人资料
                </el-dropdown-item>
                <el-dropdown-item command="change-password">
                  <el-icon><Lock /></el-icon>
                  修改密码
                </el-dropdown-item>
                <el-dropdown-item command="logout" divided>
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 主内容 -->
      <el-main class="main-content">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { computed, onMounted, onUnmounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useWebSocketStore } from '@/stores/websocket'
import { useAuthStore } from '@/stores/auth.js'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Monitor, Setting, User, ArrowDown, SwitchButton,
  Lock, Bell,
  VideoCamera, Delete, Warning, Document, View, TrendCharts, Picture,
  Timer, Files, Download, Notebook, Tools, Camera
} from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const wsStore = useWebSocketStore()
const authStore = useAuthStore()

// 当前页面标题
const currentPageTitle = computed(() => {
  return route.meta?.title || '未知页面'
})

// WebSocket连接状态
const connectionStatus = computed(() => wsStore.connectionStatus)

const connectionStatusText = computed(() => {
  const statusMap = {
    connected: 'WebSocket已连接',
    connecting: 'WebSocket连接中...',
    disconnected: 'WebSocket已断开'
  }
  return statusMap[connectionStatus.value] || '未知状态'
})

const connectionStatusColor = computed(() => {
  const colorMap = {
    connected: '#67C23A',
    connecting: '#E6A23C',
    disconnected: '#F56C6C'
  }
  return colorMap[connectionStatus.value] || '#909399'
})

// 用户菜单处理
const handleUserCommand = async (command) => {
  switch (command) {
    case 'profile':
      router.push('/profile');
      break;
    case 'change-password':
      router.push('/change-password');
      break;
    case 'logout':
      try {
        await ElMessageBox.confirm(
          '确定要退出登录吗？',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )

        // 执行登出
        authStore.logout()
        ElMessage.success('已退出登录')

        // 跳转到登录页
        router.push('/login')
      } catch {
        // 用户取消登出
      }
      break
  }
}

// 生命周期
onMounted(() => {
  wsStore.initWebSocket()
  // 刷新用户信息以获取最新数据
  authStore.refreshUserInfo()
})

onUnmounted(() => {
  wsStore.closeConnection()
})
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.sidebar {
  background-color: #304156;
  overflow: hidden;
}

.logo {
  display: flex;
  align-items: center;
  padding: 20px;
  color: #fff;
  font-size: 20px;
  font-weight: bold;
  border-bottom: 1px solid #434a50;
}

.logo-icon {
  font-size: 26px;
  margin-right: 12px;
  color: #409EFF;
}

.logo-text {
  color: #fff;
}

.sidebar-scroll {
  height: calc(100vh - 60px);
  overflow-y: auto;
  overflow-x: hidden;
}

.sidebar-scroll::-webkit-scrollbar {
  width: 6px;
}

.sidebar-scroll::-webkit-scrollbar-track {
  background: #2c3e50;
}

.sidebar-scroll::-webkit-scrollbar-thumb {
  background: #409EFF;
  border-radius: 3px;
}

.sidebar-scroll::-webkit-scrollbar-thumb:hover {
  background: #66b1ff;
}

.sidebar-menu {
  border: none;
  min-height: 100%;
}

/* 增大导航栏菜单项的字号 */
.sidebar-menu :deep(.el-menu-item),
.sidebar-menu :deep(.el-sub-menu__title) {
  font-size: 15px;
}

.sidebar-menu :deep(.el-menu-item span),
.sidebar-menu :deep(.el-sub-menu__title span) {
  font-size: 15px;
}

.header {
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
}

.header-left {
  flex: 1;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.connection-badge {
  cursor: pointer;
}

.connection-icon {
  font-size: 18px;
  transition: color 0.3s;
}

.connection-icon.connected {
  color: #67C23A;
}

.connection-icon.connecting {
  color: #E6A23C;
}

.connection-icon.disconnected {
  color: #F56C6C;
}

.user-dropdown {
  cursor: pointer;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #606266;
  font-size: 14px;
}

.main-content {
  background: #f5f7fa;
  padding: 20px;
  overflow-y: auto;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    width: 200px !important;
  }

  .logo {
    padding: 15px;
    font-size: 16px;
  }

  .header {
    padding: 0 15px;
  }

  .main-content {
    padding: 15px;
  }
}
</style>
